package com.ooqiu.gaming.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.IdRes;
import android.support.v7.widget.AppCompatRadioButton;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.ooqiu.gaming.R;
import com.ooqiu.gaming.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eenie
 * @date 2017/11/24
 */

public class WildRadioButton extends AppCompatRadioButton {
    @IdRes
    private int mJudgeId;
    private Context mContext;
    private ViewGroup mParentGroup;
    private WildRadioButton mWildRadioButton;
    private Boolean isActor = false;
    private boolean isFirstLoad = true;
    private List<WildRadioButton> mActors;
    private WildRadioButton mCurCheck;


    public WildRadioButton(Context context) {
        super(context);
        init(context, null);
    }

    public WildRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public WildRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        if (attrs != null) {
            TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.WildRadioButton);
            mJudgeId = a.getResourceId(R.styleable.WildRadioButton_wild_parent_id, 0);
            isActor = !(mJudgeId == 0);
            a.recycle();
        }

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }


    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);


        if (mParentGroup == null && mWildRadioButton == null && isFirstLoad && isActor) {
            mParentGroup = getParentGroup();
            if (mParentGroup != null) {
                mWildRadioButton = getHolderWildView();
                if (mWildRadioButton != null) {
                    mWildRadioButton.addActor(this);
                }
            }
            isFirstLoad = false;
        }

        if (checked && mWildRadioButton != null) {
            toggleCurCheck(this);
        }

//        LogUtil.e(String.format("setChecked => %s \n mParentGroup = %s \n mWildRadioButton = %s \n isFirstLoad = %s \n isActor = %s", getText(), mParentGroup == null, mWildRadioButton == null, isFirstLoad, isActor));
    }


    private ViewGroup getParentGroup() {
        ViewGroup viewGroup = (ViewGroup) getParent();
        if (viewGroup.getId() == mJudgeId) {
            return viewGroup;
        } else {
            viewGroup = (ViewGroup) viewGroup.getParent();
            if (viewGroup != null && viewGroup.getId() == mJudgeId) {
                return viewGroup;
            } else {
                throw new IllegalStateException("no found parent");
            }
        }
    }

    private WildRadioButton getHolderWildView() {
        WildRadioButton wildRadioButton = null;
        for (int i = 0; i < mParentGroup.getChildCount(); i++) {
            View tempView = mParentGroup.getChildAt(i);
            if (tempView instanceof WildRadioButton && ((WildRadioButton) tempView).getActor()) {
                wildRadioButton = (WildRadioButton) tempView;
            }
        }
        return wildRadioButton;
    }


    public Boolean getActor() {
        return isActor;
    }

    public void setActor(Boolean actor) {
        isActor = actor;
    }


    public void addActor(WildRadioButton actor) {
        if (mActors == null) {
            mActors = new ArrayList<>();
        }
        mActors.add(actor);
        LogUtil.e("actors => " + mActors.size());
    }


    public void toggleCurCheck(WildRadioButton wildRadioButton) {
        if (mWildRadioButton.getCurCheck() != null) {
            mWildRadioButton.getCurCheck().setChecked(false);
        }
        if (mWildRadioButton != null) {
            mWildRadioButton.setCurCheck(wildRadioButton);
            mCurCheck = wildRadioButton;
        }
    }

    public void setCurCheck(WildRadioButton curCheck) {
        mCurCheck = curCheck;
    }

    public WildRadioButton getCurCheck() {
        return mCurCheck;
    }


}
